#include "login.h"
#include "qcryptographichash.h"
#include "qevent.h"
#include "qjsondocument.h"
#include "qjsonobject.h"
#include <QFile>
#include <QRegularExpressionValidator>
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    QFile file{"./info.json"};
    if(file.open(QFile::ReadWrite)){
        auto json=file.readAll();
        QJsonDocument doc=QJsonDocument::fromJson(json);
        ui->lineEdit_user->setText(doc["name"].toString());
        ui->lineEdit_password->setText(doc["password"].toString());
        std::string s=doc["ip"].toString().toStdString();
        isok=Sql->connect(doc["sqluser"].toString().toStdString(),doc["sqlpassword"].toString().toStdString(),doc["dbname"].toString().toStdString(),s,doc["port"].toInt());
        obj=doc.object();
        file.close();
    }
    QRegularExpression regExp("^([a-z]|[A-Z]|[0-9])+$"); // 正则表达式，只允许输入数字和空格
    QRegularExpressionValidator* expvalidator = new QRegularExpressionValidator(regExp, ui->lineEdit_password);
    ui->lineEdit_password->setValidator(expvalidator);
}
void Login::mousePressEvent(QMouseEvent *event)
{
    if(ui->widget->underMouse()||ui->frame->underMouse())
    {
        this->windowPos = this->pos();       // 获得部件当前位置
        this->mousePos = event->globalPosition().toPoint(); // 获得鼠标位置
        this->dPos = mousePos - windowPos;   // 移动后部件所在的位置
        optFor=true;
    }else{
        optFor=false;
    }
}

void Login::mouseMoveEvent(QMouseEvent *event)
{
    if(optFor)
        this->move(event->globalPosition().toPoint() - this->dPos);
}

Login::~Login()
{
    delete ui;
}

void Login::on_minBut_clicked()
{
    showMinimized();
}


void Login::on_closeBut_clicked()
{
    close();
}


void Login::on_loginBut_clicked()
{
    auto name = ui->lineEdit_user->text();
    auto password = ui->lineEdit_password->text();
    bool ok=true;
    bool re=name.isNull();
    ok = ok&&re;
    if(re){
        showCWUser("未设置用户名");
    }
    re=password.size()<8;
    ok = ok&&re;
    if(re){
        showCWPassword("密码不少于8位");
    }
    if(ok){
        return;
    }
    if(!isok){
        showCWUser("数据库连接失败");
        return;
    }
    auto s="select * from user_password where name=\""+name+"\"";
    re=Sql->query(s.toStdString());
    if(!re){
        showCWUser("查询失败");
        return;
    }
    if(!Sql->next()){
        showCWUser("用户不存在");
        return;
    }
    auto hashPassBase64=QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md5).toBase64();
    if(Sql->value(1)!=hashPassBase64.toStdString()){
        showCWPassword("密码错误");
        return;
    }
    if(ui->checkBox->isChecked()){
        saveInfo(name,password);
    }
    emit loginSignal(std::move(name),Sql);
    this->close();
}

void Login::saveInfo(QString &name, QString &password)
{
    obj["name"]=name;
    obj["password"]=password;
    QJsonDocument doc{obj};
    auto json=doc.toJson();
    QFile file{"./info.json"};
    if(file.open(QFile::ReadWrite)){
        file.write(json);
        file.close();
    }
}

void Login::showCW(QLabel *label, QString &&s)
{
    auto stye=QString(
        R"(border-radius:none;
        background:transparent;
        border-bottom:2px  solid rgb(200, 0, 0);
        color: rgb(200, 0, 0);
        font: 11pt "楷体";")");
    label->setStyleSheet(stye);
    label->setText(s);
}

void Login::showCWUser(QString &&s)
{
    ui->lineEdit_user->setStyleSheet("border-bottom:2px  solid rgb(200, 0, 0);");
    ui->label_user_cw->setText(s);
    userIsOk=false;
}

void Login::showCWPassword(QString &&s)
{
    ui->lineEdit_password->setStyleSheet("border-bottom:2px  solid rgb(200, 0, 0);");
    ui->label_password_cw->setText(s);
    passIsOk=false;
}

void Login::on_lineEdit_user_textChanged(const QString &arg1)
{
    if(userIsOk){
        return;
    }else{
        ui->lineEdit_user->setStyleSheet("border-bottom:2px  solid rgb(255, 255, 255);");
        userIsOk=true;
        ui->label_user_cw->setText("");
    }
}


void Login::on_lineEdit_password_textChanged(const QString &arg1)
{
    if(passIsOk){
        return;
    }else{
        ui->lineEdit_password->setStyleSheet("border-bottom:2px  solid rgb(255, 255, 255);");
        passIsOk=true;
        ui->label_password_cw->setText("");
    }
}

